#!/bin/sh /etc/rc.common
# Author=wulishui , 20200908-0915 ; <wulishui@gmail.com> 
# 20210514 by sirpdboy

START=55


up_cron() {
if [ "`grep 'option enable .1.' /etc/config/rebootschedule 2>/dev/null`" ]; then
	[ -s /etc/crontabs/root ] || mcronrst=1
	del_cron
	sum=$(grep -c 'config crontab' /etc/config/rebootschedule 2>/dev/null)
	for i in $(seq 0 $((sum-1))); do
		enable=`uci get rebootschedule.@crontab[$i].enable 2>/dev/null`
		if [ $enable -eq 1 ]; then
			month=`uci get rebootschedule.@crontab[$i].month 2>/dev/null` || month="*"
			day=`uci get rebootschedule.@crontab[$i].day 2>/dev/null` || day="*"
			week=`uci get rebootschedule.@crontab[$i].week 2>/dev/null` || week="*"
			#[ ! "$day" = "*" ] && week="*"
			hour=`uci get rebootschedule.@crontab[$i].hour 2>/dev/null` || hour="*"; [ "$hour" = "0" ] && hour="00"
			minute=`uci get rebootschedule.@crontab[$i].minute 2>/dev/null` || minute="00"; [ "$minute" = "0" ] && minute="00"
			command=`uci get rebootschedule.@crontab[$i].command 2>/dev/null` || command="echo "Reboot schedule tested.""
			echo "$minute $hour $day $month $week $command #rebootschedule" >> /etc/crontabs/root
		fi
	done
	[ $mcronrst ] && /etc/init.d/cron restart
fi
}

del_cron() {
	sed -i '/rebootschedule/d' /etc/crontabs/root 2>/dev/nul
}


start()
{       
        
	if [ ! -e /etc/config/rebootschedule ]; then
	    logger -p daemon.error -t 'rebootschedule' "missing config /etc/config/rebootschedule!"
	exit 1
	fi
	up_cron
}

stop()
{
    del_cron
}
